@extends('admin.layouts.content')
@section('content')
    <form class="layui-form layui-form-pane" id="add-form" lay-filter="add-form">
        <input type="hidden" value="{{ @$model->id }}" name="id">
        <div class="layui-form-item" style="margin-top: 5px;">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-inline">
                <input type="text" name="title" required lay-verify="required" placeholder="标题"
                       autocomplete="off" class="layui-input" value="{{ @$model->title }}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图文内容</label>
            <div id="test-editormd">
                <textarea name="test-editormd" style="display:none;">{{@$model->content}}</textarea>
            </div>
            @include('markdown::encode',['editors'=>['test-editormd']])
        </div>
        <div class="layui-form-item">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否推送</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="是" {{ (@$model->status ==1) ? 'checked' : '' }}>
                <input type="radio" name="status" value="2" title="否" {{ (@$model->status !=1) ? 'checked' : '' }}>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
@endsection
<!-- 配置文件 -->

@section('script')

    <script>
        var testEditor = editormd("test-editormd",{
            width :"90%",
            height :640,
            path :"/../vendor/markdown/lib/",
            saveHTMLToTextarea :true
        });
        testEditor.getHTML();// 获取 Textarea 保存的 HTML 源码\
    </script>

    <script>
        layui.use(['jquery', 'form', 'layer'], function () {
            $ = layui.jquery;
            var form = layui.form;
            var layer = layui.layer;
            //定义layedit
            var commitStatus = true;



            form.on('submit(formSubmit)', function (data) {
                if (commitStatus) {
                    var index = parent.layer.getFrameIndex(window.name);
                    commitStatus = false;

                    var submitUrl = data.field.id ? '{{ url("admin/api/material/create-api") }}' : '{{ url("admin/api/material/create-api") }}';
                    var postData = data.field;

                    $.ajax({
                        headers: {'X-CSRF-TOKEN': "{{ csrf_token() }}"},
                        url: submitUrl,
                        method: "POST",
                        dataType: "json",
                        data: postData,
                        success: function (obj) {
                            commitStatus = true;
                            if (obj.error === 0) {
                                layer.msg('保存成功', {icon: 6});
                                parent.layui.table.reload('data-table'); //重载表格
                                parent.layer.close(index);
                            } else {
                                layer.msg(obj.msg, {icon: 5});
                            }
                        },
                        error: function (e) {
                            layer.msg('服务器错误', {icon: 5});
                        }
                    });
                }
                return false;
            });


            $('.layui-btns .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });

        function AgentSelectedCallback(s, obj) {
            if (s == 1) {
                $("#agent_id").val(obj.id);
                $("#agent_name").val(obj.name);
                layer.closeAll();
            }
        }
    </script>
@endsection
